import React, { Component } from 'react'
import { Switch, Route, Redirect } from 'react-router-dom'
import { isLogin } from './utils/index'
import Home from './pages/Home'
import Cates from './pages/Cates'
import Cart from './pages/Cart'
import User from './pages/User'
import NotFind from './pages/404'
import Search from './pages/Search'
import Detail from './pages/Detail'
import ItemList from './pages/ItemList'
import Login from './pages/Login'
import Register from './pages/Register'
import Setuser from './pages/Setuser'
import SearchRes from './pages/SearchRes'
import Favor from './pages/Favor'


export default class App extends Component {
  render() {
    return (
      <Switch>
        <Route path="/home" component={Home} />
        <Route path="/cates" component={Cates} />
        <Route path="/cart" render={(routeProps) => {
          if (isLogin()) {
            // 登录了
            return <Cart {...routeProps} />
          } else {
            // 没登陆
            return <Redirect to={{
              pathname: '/login',
              state: {
                from: '/cart'
              }
            }} />
          }
        }} />
        <Route path="/user" render={(routeProps) => {
          if (isLogin()) {
            // 登录了
            return <User {...routeProps} />
          } else {
            // 没登陆
            return <Redirect to={{
              pathname: '/login',
              state: {
                from: '/user'
              }
            }} />
          }
        }} />
        <Route path="/setuser" render={(routeProps) => {
          if (isLogin()) {
            // 登录了
            return <Setuser {...routeProps} />
          } else {
            // 没登陆
            return <Redirect to={{
              pathname: '/setuser',
              state: {
                from: '/user'
              }
            }} />
          }
        }} />
        <Route path="/favor" render={(routeProps) => {
          if (isLogin()) {
            // 登录了
            return <Favor {...routeProps} />
          } else {
            // 没登陆
            return <Redirect to={{
              pathname: '/favor',
              state: {
                from: '/user'
              }
            }} />
          }
        }} />
        <Route path="/search" component={Search} />
        <Route path="/searchres" component={SearchRes} />
        <Route path="/detail/:id" component={Detail} />
        <Route path="/itemlist/:id" component={ItemList} />
        <Route path="/login" component={Login} />
        <Route path="/Register" component={Register} />
        <Route path="/404" component={NotFind} />
        <Redirect to="/home" from="/" exact />
        <Redirect to="/404" />
      </Switch>
    )
  }
}

